package com.test12;

class TicketThread implements Runnable {
    private volatile int number = 50;   // 总票数

    @Override
    public void run() {
        while (this.sale()) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private synchronized boolean sale() {   // 卖票
        if (this.number > 0) {
            System.out.println(Thread.currentThread().getName() + "：卖出第" + (this.number--) + "张票");
            return true;
        }
        return false;
    }
}

public class MyTest_01 {

    public static void main(String[] args) {
        TicketThread body = new TicketThread();
        for (int i = 0; i < 2; i++) {
            new Thread(body, "○售票厅-" + i).start();
        }
        for (int i = 0; i < 3; i++) {
            new Thread(body, "△票贩子-" + i).start();
        }
    }
}
